<?php
$DatesRequired = false;

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

if (isset($criteria) && ($criteria->start_datetime && $criteria->end_datetime)){
    /**
     * This uses the checks in the price calculator to check the start day and
     * minimum number of days are ok.
     */
    if ($criteria->start_datetime && $criteria->end_datetime) {
        $period = new RM_Reservation_Period(
            new RM_Date(strtotime($criteria->start_datetime)),
            new RM_Date(strtotime($criteria->end_datetime))
        );
    }

    $persons = new RM_Reservation_Persons(array("adults"=>$criteria->adults,"children"=>$criteria->children,"infants"=>$criteria->infants));

    $information = new RM_Prices_Information($this->unit, $period, $persons);
    $priceSystem = RM_Environment::getInstance()->getPriceSystem();


    try {
        $showAddToCart = $priceSystem->getTotalUnitPrice($information);
    } catch (Exception $e) {
        $DatesRequired = true;
    }

    /**
     * this checks if the unit has any partial reservations
     * This is only used when the flexible criteria Excursion is selected.
     */
    if ($criteria->flexible && !$DatesRequired){
        $unitDAO = new RM_Units();
        $reservations = $unitDAO->isFlexibleAvailable($this->unit, $period);

        if (!empty($reservations)){
            $DatesRequired = true;
        }
    }
} else {
    $DatesRequired = true;
}

$isSelected = (RM_Reservation_Manager::getInstance()->getDetails($this->unit) !== null) ? 1 : 0;

// panel config
$panelsettings = Zend_Json::decode($this->panel->settings);
$align = $panelsettings['align'];
$config = new RM_Config;

?>
<div id="<?php echo $this->panel->getDivID() ?>">
    <div class="RM_unit_details_cartandbuynow_container" style="float:<?php echo $panelsettings['align'];?>">
        <div id="RM_qtyselection" class="RM_unit_details_qtyselection"></div>
        <span class="RM_unit_details_booknowbutton">
            <?php
            if ($DatesRequired) {
                if ($isSelected) { ?>
                    <input onclick="rm_unit_details_selectDatesConfirm()" type="button" value="<?php echo $this->_translate->_('User.Unit.Details','CheckAvailability');?>" />
                <?php } else { ?>
                    <input onclick="rm_unit_details_selectDates(datepickerURL,datepickerWidth,datepickerHeight)" value="<?php echo $this->_translate->_('User.Unit.Details','CheckAvailability');?>" type="button" />
                <?php }
            } else {
                ?>
                <input type="button" id="RM_addtocart_button" onclick="rm_unit_details_addtocart(RM.Pages.User.Unit_id,RM.Pages.User.SubUnits_id,false)" value="<?php echo $this->_translate->_('User.Unit.Details','AddtoCart');?>" />
                <?php
            }
            ?>
        </span>
    </div>
</div>
<div class="RM_clear"></div>
<script type="text/javascript">
rm_unit_details_selectDatesConfirm = function(){
    if (confirm('<?php echo $this->_translate->_('User.Unit.List','AlreadyInCart'); ?>')) {
        rm_unit_details_selectDates(datepickerURL,datepickerWidth,datepickerHeight);
    }
}
</script>


